package com.simon.app;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;

import com.simon.app.consts.GV;
import com.simon.app.service.AdDogService;
import com.simon.app.utils.LogUtil;
import com.simon.app.utils.NotificationHelper;
import com.simon.app.utils.StoreUtil;
import com.simon.app.utils.ToastUtil;

public class MainActivity extends AppCompatActivity {
    private TextView mServiceStatusTxt;
    private ImageButton mServiceStatusImageBtn;

    private Switch mMenuBtnStartUpAdListen;
    private Switch mMenuBtnDialogAdListen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initCpt();
        initMenu();

        Intent intentMain = new Intent(this, MainActivity.class);
        NotificationHelper notificationHelper = new NotificationHelper(this);
        notificationHelper.sendNotification("广告狗", "正在运行", 1, intentMain);
    }

    //  初始化組件
    private void initCpt() {
        mServiceStatusTxt = findViewById(R.id.service_status_txt);
        mServiceStatusImageBtn = findViewById(R.id.service_status_image);

        // 启动服务
        mServiceStatusImageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(intent);
            }
        });
    }

    // 初始化菜单
    private void initMenu() {
        // 开屏广告 设置
        SharedPreferences.Editor startUpAdEditor = StoreUtil.getEditor(this, GV.CFG_SERVICE_START_UP_AD_STATUS);
        mMenuBtnStartUpAdListen = findViewById(R.id.sw_start_up_ad_listen);
        mMenuBtnStartUpAdListen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    // 开启 开屏广告 监听
                    if (AdDogService.isRunning()) {
                        AdDogService.mStatusStartUpAdListen = true;
                        startUpAdEditor.putBoolean(GV.CFG_SERVICE_START_UP_AD_STATUS, true); // 保存配置
                        mMenuBtnStartUpAdListen.setChecked(true);
                        ToastUtil.showShortToast(MainActivity.this, "开启 开屏广告监听");

                    } else {
                        mMenuBtnStartUpAdListen.setChecked(false);
                        showDialog();
                    }

                } else {
                    // 关闭 开屏广告 监听
                    AdDogService.mStatusStartUpAdListen = false;
                    mMenuBtnStartUpAdListen.setChecked(false);
                    // 保存配置
                    startUpAdEditor.putBoolean(GV.CFG_SERVICE_START_UP_AD_STATUS, false);
                }

                // 保存状态
                startUpAdEditor.apply();
            }
        });

        // 弹窗广告 设置
        SharedPreferences.Editor dialogAdEditor = StoreUtil.getEditor(this, GV.CFG_SERVICE_DIALOG_AD_STATUS);
        mMenuBtnDialogAdListen = findViewById(R.id.sw_dialog_ad_listen);
        mMenuBtnDialogAdListen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    if (AdDogService.isRunning()) {
                        AdDogService.mStatusDialogAdListen = true;
                        dialogAdEditor.putBoolean(GV.CFG_SERVICE_DIALOG_AD_STATUS, true); // 保存配置
                        mMenuBtnDialogAdListen.setChecked(true);
                        ToastUtil.showShortToast(MainActivity.this, "开启 弹窗监听");

                    } else {
                        mMenuBtnDialogAdListen.setChecked(false);
                        showDialog();
                    }

                } else {
                    // 关闭 开屏广告 监听
                    AdDogService.mStatusDialogAdListen = false;
                    mMenuBtnDialogAdListen.setChecked(false);
                    dialogAdEditor.putBoolean(GV.CFG_SERVICE_DIALOG_AD_STATUS, false);
                }

                // 保存状态
                dialogAdEditor.apply();
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.i("onResume ==> AdDogService.isRunning()==>" + AdDogService.isRunning());
        if (AdDogService.isRunning()) {
            // 无障碍服务 【开启】 设置图片文字 状态
            mServiceStatusTxt.setText(getResources().getText(R.string.service_status_ok));
            mServiceStatusTxt.setTextColor(getResources().getColor(R.color.blue));
            mServiceStatusImageBtn.setImageResource(R.drawable.link);

            // 开屏广告 状态设置
            AdDogService.mStatusStartUpAdListen = StoreUtil.getBoolean(this, GV.CFG_SERVICE_START_UP_AD_STATUS);
            mMenuBtnStartUpAdListen.setChecked(AdDogService.mStatusStartUpAdListen);

            // 弹窗广告 状态设置
            AdDogService.mStatusDialogAdListen = StoreUtil.getBoolean(this, GV.CFG_SERVICE_DIALOG_AD_STATUS);
            mMenuBtnDialogAdListen.setChecked(AdDogService.mStatusDialogAdListen);

        } else {
            // 无障碍服务 【未启动】 设置图片文字 状态
            mServiceStatusTxt.setText(getResources().getText(R.string.service_status_no));
            mServiceStatusTxt.setTextColor(getResources().getColor(R.color.red));
            mServiceStatusImageBtn.setImageResource(R.drawable.linkun);

            // 开屏 广告状态设置
            AdDogService.mStatusStartUpAdListen = false;
            mMenuBtnStartUpAdListen.setChecked(false);

            // 弹窗 广告状态设置
            AdDogService.mStatusDialogAdListen = false;
            mMenuBtnDialogAdListen.setChecked(false);
        }
    }

    // 未开启服务 弹窗提示
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        // builder.setTitle("无障碍服务检测");
        builder.setMessage("无障碍服务未开启,会导致脚本无法使用!");

        builder.setPositiveButton("去开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(intent);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showLongToast(MainActivity.this, "已取消,如需使用,请开启无障碍服务!!!");
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

}